////////////////////////////////////////////////////////////////////////////
//
//  Crytek Engine Source File.
//  Copyright (C), Crytek Studios, 2002.
// -------------------------------------------------------------------------
//  File name:   terrain_det_tex.cpp
//  Version:     v1.00
//  Created:     28/5/2001 by Vladimir Kajalin
//  Compilers:   Visual Studio.NET
//  Description: terrain detail textures
// -------------------------------------------------------------------------
//  History:
//
////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "terrain.h"
#include "terrain_sector.h"
#include "3dEngine.h"

void CTerrain::SetDetailLayerProperties(int nId, uint8 ucProjAxis, const PodArray<int> & lstnVegetationGroups, IMaterial * pMat, int nSID)
{
	if(nId>=0 && nId<MAX_SURFACE_TYPES_COUNT)
	{
		m_SSurfaceType[nSID][nId].ucDefProjAxis = ucProjAxis;
		m_SSurfaceType[nSID][nId].ucThisSurfaceTypeId = nId;
		m_SSurfaceType[nSID][nId].lstnVegetationGroups.Reset();
		m_SSurfaceType[nSID][nId].lstnVegetationGroups.AddList(lstnVegetationGroups);
		m_SSurfaceType[nSID][nId].pLayerMat = (CMatInfo*)pMat;
		if(pMat && m_bEditor && !Get3DEngine()->IsSegmentedWorldActive())
			CTerrain::Get3DEngine()->PrintMessage("  Layer %d - has material %s", nId, pMat->GetName());
	}
	else
	{
		Warning("CTerrain::SetDetailTextures: LayerId is out of range: %d: %s", nId, pMat ? pMat->GetName() : "unknown");
		assert(!"CTerrain::SetDetailTextures: LayerId is out of range");
	}
}

void CTerrain::LoadSurfaceTypesFromXML(XmlNodeRef pDetTexTagList, int nSID)
{
  LOADING_TIME_PROFILE_SECTION;

	MEMSTAT_CONTEXT(EMemStatContextTypes::MSC_Terrain, 0, "Surface types");

	if (!Get3DEngine()->IsSegmentedWorldActive() && !gEnv->IsDedicated())
		CTerrain::Get3DEngine()->PrintMessage("Loading terrain layers for segment %d ...", nSID);

 	if (!pDetTexTagList)
		return;

	for (int nId = 0; nId < pDetTexTagList->getChildCount() && nId < MAX_SURFACE_TYPES_COUNT; nId++)
	{
		XmlNodeRef pDetLayer = pDetTexTagList->getChild(nId);
		IMaterialManager* pMatMan = Get3DEngine()->GetMaterialManager();
		const char * pMatName = pDetLayer->getAttr("DetailMaterial");
		IMaterial * pMat = (pMatName[0] && !(GetCVars()->e_VoxTer && !m_bEditor)) ? pMatMan->LoadMaterial(pMatName) : NULL;

		uint8 projAxis = pDetLayer->getAttr("ProjAxis")[0];

		if(!pMat || pMat == pMatMan->GetDefaultMaterial())
		{
      if(!GetCVars()->e_VoxTer && !gEnv->IsDedicated())
			  Error("CTerrain::LoadSurfaceTypesFromXML: Error loading material: %s", pMatName);
			pMat = pMatMan->GetDefaultTerrainLayerMaterial();
		}

		if(CMatInfo * pMatInfo = (CMatInfo *)pMat)
		{
			pMatInfo->m_fDefautMappingScale = 1;
			Get3DEngine()->InitMaterialDefautMappingAxis(pMatInfo);
		}

		PodArray<int> lstnVegetationGroups;
		for (int nGroup = 0; nGroup < pDetLayer->getChildCount(); nGroup++)
		{
			XmlNodeRef pVegetationGroup = pDetLayer->getChild(nGroup);
			int nVegetationGroupId=-1;
			if(pVegetationGroup->isTag("VegetationGroup") && pVegetationGroup->getAttr("Id",nVegetationGroupId))
				lstnVegetationGroups.Add(nVegetationGroupId);
		}

		SetDetailLayerProperties(nId, projAxis, lstnVegetationGroups, pMat, nSID);
	}
}

void CTerrain::UpdateSurfaceTypes(int nSID)
{
	if(GetParentNode(nSID))
		GetParentNode(nSID)->UpdateDetailLayersInfo(true);
}
